#include "SettingsManagerSingleton.h"
#include <QDir>

//================================================
//================================================
QSharedPointer<SettingsManagerSingleton> SettingsManagerSingleton::instance;
//================================================
SettingsManagerSingleton::SettingsManagerSingleton(QObject *parent) :
    QObject(parent){
    QString currentPath = QDir::currentPath();
    this->settings = new QSettings("WebcamRecorder", currentPath, this);
}
//================================================
SettingsManagerSingleton *SettingsManagerSingleton::getInstance(){
    if(SettingsManagerSingleton::instance.isNull()){
        QSharedPointer<SettingsManagerSingleton> localInstance(new SettingsManagerSingleton);
        SettingsManagerSingleton::instance = localInstance;
    }
    SettingsManagerSingleton *pointer = SettingsManagerSingleton::instance.data();
    return pointer;
}
//===============================================
//================================================
QString SettingsManagerSingleton::getLanguage(){
    QString language = this->settings->value(KEY_LANG, "en").toString();
    return language;
}
//================================================
QString SettingsManagerSingleton::getImagesTestFilePath(){
    QString defaultPath = QDir::currentPath() + QDir::separator() + "imageEmailTest.jpg";
    return defaultPath;
}
//================================================
QString SettingsManagerSingleton::getImagesDirPath(){
    QString defaultPath = QDir::currentPath() + QDir::separator() + "images" + QDir::separator();
    QString imagesDirPath = this->settings->value(KEY_DIR_IMAGES, defaultPath).toString();
    return imagesDirPath;
}
//================================================
QString SettingsManagerSingleton::getVideosDirPath(){
    QString defaultPath = QDir::currentPath() + QDir::separator() + "videos" + QDir::separator();
    QString videosDirPath = this->settings->value(KEY_DIR_VIDEOS, defaultPath).toString();
    return videosDirPath;
}
//================================================
QString SettingsManagerSingleton::getAutoSnappingDirPath(){
    QString defaultPath = QDir::currentPath() + QDir::separator() + "autoSnapping" + QDir::separator();
    QString autoSnappingDirPath = this->settings->value(KEY_DIR_AUTOSNAPPING, defaultPath).toString();
    return autoSnappingDirPath;
}
//================================================
QString SettingsManagerSingleton::getMotionDirPath(){
    QString defaultPath = QDir::currentPath() + QDir::separator() + "motion" + QDir::separator();
    QString motionDirPath = this->settings->value(KEY_DIR_MOTION, defaultPath).toString();
    return motionDirPath;
}
//================================================
QString SettingsManagerSingleton::getServerType(){
    QString defaultValue = "smtp";
    QString serverType = this->settings->value(KEY_EMAIL_SERVERTYPE, defaultValue).toString();
    return serverType;
}
//================================================
int SettingsManagerSingleton::getServerPort(){
    int defaultValue = 587;
    int serverPort = this->settings->value(KEY_EMAIL_SERVERPORT, defaultValue).toInt();
    return serverPort;
}
//================================================
QString SettingsManagerSingleton::getServerAddress(){
    QString defaultValue = "smtp.gmail.com";
    QString serverAddress = this->settings->value(KEY_EMAIL_SERVERADDRESS, defaultValue).toString();
    return serverAddress;
}
//================================================
QString SettingsManagerSingleton::getEmailSender(){
    QString defaultValue = "";
    QString emailSender = this->settings->value(KEY_EMAIL_SENDER, defaultValue).toString();
    return emailSender;
}
//================================================
QString SettingsManagerSingleton::getPasswordSender(){
    bool rememberPasswordSender = !this->getDontRememberPasswordSender();
    if(rememberPasswordSender){
        QString defaultValue = "";
        this->passwordSender = this->settings->value(KEY_EMAIL_SENDERPWD, defaultValue).toString();
    }
    return this->passwordSender;
}
//================================================
QString SettingsManagerSingleton::getEmailReceivers(){
    QString defaultValue = "";
    QString receivers = this->settings->value(KEY_EMAIL_RECEIVERS, defaultValue).toString();
    return receivers;
}
//================================================
bool SettingsManagerSingleton::getHidePasswordSender(){
    bool defaultValue = true;
    bool hidePassword = this->settings->value(KEY_EMAIL_HIDEPWD, defaultValue).toBool();
    return hidePassword;
}
//================================================
bool SettingsManagerSingleton::getDontRememberPasswordSender(){
    bool defaultValue = true;
    bool dontRemember = this->settings->value(KEY_EMAIL_DONTREMEMBERPWD, defaultValue).toBool();
    return dontRemember;
}
//================================================
QTime SettingsManagerSingleton::getAutoSnappingTimeInterval(){
    QTime defaultValue(0, 0, 0, 0);
    defaultValue = defaultValue.addSecs(600); //TODO change that default value in the final version
    QTime interval = this->settings->value(KEY_AUTOSNAPPING_INTERVAL, defaultValue).toTime();
    return interval;
}
//================================================
int SettingsManagerSingleton::getAutoSnappingTimeIntervalInMs(){
    QTime nullTime(0, 0, 0, 0);
    QTime timeInterval
        = this->getAutoSnappingTimeInterval();
    int timeIntervalInMs = nullTime.msecsTo(timeInterval);
    return timeIntervalInMs;
}
//================================================
bool SettingsManagerSingleton::getAutoSnappingSendEmail(){
    bool defaultValue = true;
    bool sendEmail = this->settings->value(KEY_AUTOSNAPPING_SENDEMAIL, defaultValue).toBool();
    return sendEmail;
}
//================================================
int SettingsManagerSingleton::getMotionTimeInterval(){
    int defaultValue = 1000;
    int ms = this->settings->value(KEY_MOTION_INTERVAL, defaultValue).toInt();
    return ms;
}
//================================================
int SettingsManagerSingleton::getMotionSensibility(){
    int defaultValue = 50;
    int motionSensibility = this->settings->value(KEY_MOTION_SENSIBILITY, defaultValue).toInt();
    return motionSensibility;
}
//================================================
bool SettingsManagerSingleton::getMotionSendEmail(){
    bool defaultValue = true;
    bool sendEmail = this->settings->value(KEY_MOTION_SENDEMAIL, defaultValue).toBool();
    return sendEmail;
}
//================================================
bool SettingsManagerSingleton::getMotionSendEmailWithImage(){
    bool defaultValue = true;
    bool sendImage = this->settings->value(KEY_MOTION_SENDIMAGE, defaultValue).toBool();
    return sendImage;
}
//================================================
bool SettingsManagerSingleton::getMotionSave(){
    bool defaultValue = true;
    bool save = this->settings->value(KEY_MOTION_SAVE, defaultValue).toBool();
    return save;
}
//================================================
bool SettingsManagerSingleton::getMotionSaveImage(){
    bool defaultValue = true;
    bool saveImage = this->settings->value(KEY_MOTION_SAVEIMAGE, defaultValue).toBool();
    return saveImage;
}
//================================================
bool SettingsManagerSingleton::getMotionSaveVideo(){
    bool defaultValue = true;
    bool saveVideo = this->settings->value(KEY_MOTION_SAVEVIDEO, defaultValue).toBool();
    return saveVideo;
}
//================================================
//================================================
void SettingsManagerSingleton::setLanguage(QString language){
    this->settings->setValue(KEY_LANG, language);
    emit this->languageChanged(language);
}
//================================================
void SettingsManagerSingleton::setImagesDirPath(QString imagesDirPath){
    //imagesDirPath = this->_getValidDirPath(imagesDirPath);
    this->settings->setValue(KEY_DIR_IMAGES, imagesDirPath);
    emit this->imagesDirPathChanged(imagesDirPath);
}
//================================================
QString SettingsManagerSingleton::_getValidDirPath(QString dirPath){
    if(!dirPath.isEmpty()){
        if(!dirPath.endsWith("/")
                    && !dirPath.endsWith("\\")){
            dirPath += QDir::separator();
        }
    }
    return dirPath;
}
//================================================
void SettingsManagerSingleton::setVideosDirPath(QString videosDirPath){
    //videosDirPath = this->_getValidDirPath(videosDirPath);
    this->settings->setValue(KEY_DIR_VIDEOS, videosDirPath);
    emit this->videosDirPathChanged(videosDirPath);
}
//================================================
void SettingsManagerSingleton::setAutoSnappingDirPath(QString autoSnappingDirPath){
    //autoSnappingDirPath = this->_getValidDirPath(autoSnappingDirPath);
    this->settings->setValue(KEY_DIR_AUTOSNAPPING, autoSnappingDirPath);
    emit this->autoSnappingDirPathChanged(autoSnappingDirPath);
}
//================================================
void SettingsManagerSingleton::setMotionDirPath(QString motionDirPath){
    //motionDirPath = this->_getValidDirPath(motionDirPath);
    this->settings->setValue(KEY_DIR_MOTION, motionDirPath);
    emit this->motionDirPathChanged(motionDirPath);
}
//================================================
void SettingsManagerSingleton::setServerType(QString serverType){
    this->settings->setValue(KEY_EMAIL_SERVERTYPE, serverType);
    emit this->emailInformationChanged();
}
//================================================
void SettingsManagerSingleton::setServerPort(int serverPort){
    this->settings->setValue(KEY_EMAIL_SERVERPORT, serverPort);
    emit this->emailInformationChanged();
}
//================================================
void SettingsManagerSingleton::setServerAddress(QString serverAddress){
    this->settings->setValue(KEY_EMAIL_SERVERADDRESS, serverAddress);
    emit this->emailInformationChanged();
}
//================================================
void SettingsManagerSingleton::setEmailSender(QString emailSender){
    this->settings->setValue(KEY_EMAIL_SENDER, emailSender);
    emit this->emailInformationChanged();
}
//================================================
void SettingsManagerSingleton::setPasswordSender(QString password){
    this->passwordSender = password;
    bool rememberPasswordSender = !this->getDontRememberPasswordSender();
    if(rememberPasswordSender){
        this->settings->setValue(KEY_EMAIL_SENDERPWD, password);
    }
    emit this->emailInformationChanged();
}
//================================================
void SettingsManagerSingleton::setEmailReceivers(QString receivers){
    this->settings->setValue(KEY_EMAIL_RECEIVERS, receivers);
    emit this->emailInformationChanged();
}
//================================================
void SettingsManagerSingleton::setHidePasswordSender(bool hidePassword){
    this->settings->setValue(KEY_EMAIL_HIDEPWD, hidePassword);
    emit this->hidePasswordSenderChanged(hidePassword);
}
//================================================
void SettingsManagerSingleton::setDontRememberPasswordSender(bool dontRemember){
    this->settings->setValue(KEY_EMAIL_DONTREMEMBERPWD, dontRemember);
    if(dontRemember){
        this->settings->remove(KEY_EMAIL_SENDERPWD);
    }else{
        this->settings->setValue(KEY_EMAIL_SENDERPWD, this->passwordSender);
    }
}
//================================================
void SettingsManagerSingleton::setAutoSnappingTimeInterval(QTime interval){
    this->settings->setValue(KEY_AUTOSNAPPING_INTERVAL, interval);
    emit this->autoSnappingTimeIntervalChanged(interval);
}
//================================================
void SettingsManagerSingleton::setAutoSnappingSendEmail(bool sendEmail){
    this->settings->setValue(KEY_AUTOSNAPPING_SENDEMAIL, sendEmail);
}
//================================================
void SettingsManagerSingleton::setMotionTimeInterval(int ms){
    this->settings->setValue(KEY_MOTION_INTERVAL, ms);
    emit this->motionTimeIntervalChanged(ms);
}
//================================================
void SettingsManagerSingleton::setMotionSensibility(int sensibility){
    this->settings->setValue(KEY_MOTION_SENSIBILITY, sensibility);
}
//================================================
void SettingsManagerSingleton::setMotionSendEmail(bool sendEmail){
    this->settings->setValue(KEY_MOTION_SENDEMAIL, sendEmail);
}
//================================================
void SettingsManagerSingleton::setMotionSendEmailWithImage(bool sendImage){
    this->settings->setValue(KEY_MOTION_SENDIMAGE, sendImage);
}
//================================================
void SettingsManagerSingleton::setMotionSave(bool saveImage){
    this->settings->setValue(KEY_MOTION_SAVE, saveImage);
}
//================================================
void SettingsManagerSingleton::setMotionSaveImage(bool saveImage){
    this->settings->setValue(KEY_MOTION_SAVEIMAGE, saveImage);
}
//================================================
void SettingsManagerSingleton::setMotionSaveVideo(bool saveVideo){
    this->settings->setValue(KEY_MOTION_SAVEVIDEO, saveVideo);
}
//================================================

